﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace UFrameWork
{
    /// <summary>
    /// 消息分发
    /// </summary>
    internal static class UDispatcher
    {
        public static void Do(object session,string msg)
        {
            UWork.Do(UEvent.SERVER_MSG_DISPATCH,session,msg);
            try
            {
                JObject json = JsonConvert.DeserializeObject<JObject>(msg);
                string key = json["key"].Value<string>();
                List<Type> types = UWork.GetMethodParamType(key);
                if (types == null) return;
                List<object> paramobjs = new List<object>();
                paramobjs.Add(session);
                for (int i = 1; i < types.Count; i++)
                {
                    paramobjs.Add(json["data"][i - 1].ToObject(types[i]));
                }
                UWork.Do(key, paramobjs.ToArray());
            }
            catch (Exception e)
            {
                UWork.Do(UEvent.SERVER_MSG_DISPATCH_ERROR,session, msg, e);
                ULog.Error("UDispatcher:" + msg + "\r\n" + e);
            }
        }
    }
}
